/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consys.social.core.service;

import consys.social.core.NotAuthorizedException;
import consys.social.core.bo.DiscussionPost;
import consys.social.core.bo.Entity;
import consys.social.core.bo.WallPost;
import consys.social.core.dao.DiscussionDao;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 *
 * @author petan
 */
public class DiscussionServiceImpl implements DiscussionService {

    private DiscussionDao discussionDao;
    private FollowService followService;

    public void setFollowService(FollowService followService) {
        this.followService = followService;
    }

    public void setDiscussionDao(DiscussionDao discussionDao) {
        this.discussionDao = discussionDao;
    }

    @Override
    public void postDiscussion(WallPost post, DiscussionPost discussion) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
	    if (source == null) {
            throw new IllegalArgumentException("Source cannot be null");
        }
        if (!source.equals(discussion.getAddedBy())) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (post == null) {
            throw new IllegalArgumentException("Post cannot be null");
        }
        if (discussion == null) {
            throw new IllegalArgumentException("Discussion post cannot be null");
        }
        if (!followService.isFollowing(source, post.getAddedBy())) {
            throw new NotAuthorizedException("You can post discussion only to posts of people you are following");
        }

        discussionDao.postDiscussion(post, discussion);
    }

    @Override
    public void deleteDiscussionPost(WallPost post, DiscussionPost discussion) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (source == null) {
            throw new IllegalArgumentException("Source cannot be null");
        }
        if (!source.equals(discussion.getAddedBy())) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (post == null) {
            throw new IllegalArgumentException("Post cannot be null");
        }
        if (discussion == null) {
            throw new IllegalArgumentException("Discussion post cannot be null");
        }
        if (!discussionDao.isAuthor(source, post, discussion) && !source.equals(post.getAddedBy())) {
            throw new NotAuthorizedException("You can delete only your posts or discussion posts from your profile");
        }
        discussionDao.deleteDiscussionPost(post, discussion);
    }

    @Override
    public List<DiscussionPost> loadDiscussion(WallPost post, int count, Date from) throws IOException {
        if (post == null) {
            throw new IllegalArgumentException("Post cannot be null");
        }
        return loadDiscussion(post, count, from, false);
    }

    @Override
    public List<DiscussionPost> loadDiscussionNewest(WallPost post, int count) throws IOException {
        if (post == null) {
            throw new IllegalArgumentException("Post cannot be null");
        }
        return loadDiscussion(post, count, new Date(), true);
    }

    private List<DiscussionPost> loadDiscussion(WallPost post, int count, Date from, boolean newest) throws IOException {
        return discussionDao.getDiscussion(post, count, from, newest);
    }
}